﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hlavny.modul1;

namespace hlavny.modul2
{
    [Serializable]
    public class CTable : IComparable
    {
        private int id = 0;
        private int database = 0;
        private string name = null;
        private List<int> columnsIds = null;            //list of table columns ids
        [NonSerialized]
        private List<CAttribute> columnsRefs = null;    //help list of references to table columns objects 

        #region CTable properties

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public int Database
        {
            get { return database; }
            set { database = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public List<int> ColumnsIds
        {
            get { return columnsIds; }
            set { columnsIds = value; }
        }
        public List<CAttribute> ColumnsRefs
        {
            get { return columnsRefs; }
            set { columnsRefs = value; }
        }

        #endregion

        #region CTable constructor overloads

        public CTable(int id)
        {
            this.id = id;
        }
        public CTable(int id, string name)
        {
            this.id = id;
            this.name = name;
            this.columnsIds = new List<int>();
        }
        public CTable(int id, int database, string name)
        {
            this.id = id;
            this.database = database;
            this.name = name;
            this.columnsIds = new List<int>();
        }

        #endregion

        #region CTable main functions

        /// <summary>
        /// Function add id of attribute for table
        /// </summary>
        /// <param name="attribute">Adding attribute</param>
        /// <returns>true</returns>
        public bool AddAttr(CAttribute column)
        {
            columnsIds.Add(column.Id);
            columnsIds.Sort();
            return true;
        }

        /// <summary>
        /// Function drop attribute from table
        /// </summary>
        /// <param name="attribute">Dropping attribute</param>
        /// <returns>true if attribute dropped, otherwise false</returns>
        public bool DropAttr(CAttribute attribute)
        {
            int index = columnsIds.BinarySearch(attribute.Id);

            //odstranenie id zo zoznamu
            if (index < 0)
            {
                return false;
            }
            else
            {
                columnsIds.RemoveAt(index);
                columnsIds.Sort();
            }

            //ak existuje zoznam referencii odstranenie aj referencie
            if (columnsRefs != null)
            {
                index = columnsRefs.BinarySearch(attribute);
                if (index >= 0)
                {
                    columnsRefs.RemoveAt(index);
                    columnsRefs.Sort();
                }
            }
            return true;
        }

        /// <summary>
        /// Function reload all references to table columns
        /// </summary>
        /// <param name="sysColumns">Object CSysColumns</param>
        public void ReloadColumns(CSysColumns sysColumns)
        {
            columnsRefs = new List<CAttribute>();
            foreach (int id in columnsIds) {
                int index = sysColumns.GetAttributeIndex(id);
                if (index >= 0) 
                {
                    columnsRefs.Add(sysColumns.Columns[index]);
                }
            }
            columnsRefs.Sort();
        }

        /// <summary>
        /// Function find out if the table already containts column with entered name
        /// </summary>
        /// <param name="name">name of the column</param>
        /// <param name="sysColumns">Object CSysColumns</param>
        /// <returns></returns>
        public bool ContaintColumnName(string name, CSysColumns sysColumns)
        {
            if (columnsIds.Count > 0)
            {
                foreach (int id in columnsIds)
                {
                    if (sysColumns.Columns[sysColumns.GetAttributeIndex(id)].Name == name)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            CTable tmp = obj as CTable;
            return this.id.CompareTo(tmp.Id);
        }

        #endregion

        #region ToString function override

        public override string ToString()
        {
            return this.name;
        }
        
        #endregion
    }
}
